home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Libraries / tcl7.4b3 / doc / TraceVar.3 < prev    next >
Encoding:
Text File  |  1995-02-22  |  13.8 KB  |  354 lines

  1. '\"
  2. '\" Copyright (c) 1989-1993 The Regents of the University of California.
  3. '\" Copyright (c) 1994-1995 Sun Microsystems, Inc.
  4. '\"
  5. '\" See the file "license.terms" for information on usage and redistribution
  6. '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  7. '\" 
  8. '\" @(#) TraceVar.3 1.18 95/02/22 14:37:21
  9. '\" 
  10. .so man.macros
  11. .HS Tcl_TraceVar tclc 7.4
  12. .BS
  13. .SH NAME
  14. Tcl_TraceVar, Tcl_TraceVar2, Tcl_UntraceVar, Tcl_UntraceVar2, Tcl_VarTraceInfo, Tcl_VarTraceInfo2 \- monitor accesses to a variable
  15. .SH SYNOPSIS
  16. .nf
  17. \fB#include <tcl.h>\fR
  18. .sp
  19. int
  20. \fBTcl_TraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
  21. .sp
  22. int
  23. \fBTcl_TraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
  24. .sp
  25. \fBTcl_UntraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
  26. .sp
  27. \fBTcl_UntraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
  28. .sp
  29. ClientData
  30. \fBTcl_VarTraceInfo(\fIinterp, varName, flags, proc, prevClientData\fB)\fR
  31. .sp
  32. ClientData
  33. \fBTcl_VarTraceInfo2(\fIinterp, name1, name2, flags, proc, prevClientData\fB)\fR
  34. .SH ARGUMENTS
  35. .AS Tcl_VarTraceProc prevClientData
  36. .AP Tcl_Interp *interp in
  37. Interpreter containing variable.
  38. .AP char *varName in
  39. Name of variable.  May refer to a scalar variable, to
  40. an array variable with no index, or to an array variable
  41. with a parenthesized index.
  42. .VS
  43. If the name references an element of an array, then it
  44. must be in writable memory:  Tcl will make temporary modifications 
  45. to it while looking up the name.
  46. .VE
  47. .AP int flags in
  48. OR-ed combination of the values TCL_TRACE_READS, TCL_TRACE_WRITES, and
  49. TCL_TRACE_UNSETS, and TCL_GLOBAL_ONLY.  Not all flags are used by all
  50. procedures.  See below for more information.
  51. .AP Tcl_VarTraceProc *proc in
  52. Procedure to invoke whenever one of the traced operations occurs.
  53. .AP ClientData clientData in
  54. Arbitrary one-word value to pass to \fIproc\fR.
  55. .AP char *name1 in
  56. Name of scalar or array variable (without array index).
  57. .AP char *name2 in
  58. For a trace on an element of an array, gives the index of the
  59. element.  For traces on scalar variables or on whole arrays,
  60. is NULL.
  61. .AP ClientData prevClientData in
  62. If non-NULL, gives last value returned by \fBTcl_VarTraceInfo\fR or
  63. \fBTcl_VarTraceInfo2\fR, so this call will return information about
  64. next trace.  If NULL, this call will return information about first
  65. trace.
  66. .BE
  67.  
  68. .SH DESCRIPTION
  69. .PP
  70. \fBTcl_TraceVar\fR allows a C procedure to monitor and control
  71. access to a Tcl variable, so that the C procedure is invoked
  72. whenever the variable is read or written or unset.
  73. If the trace is created successfully then \fBTcl_TraceVar\fR returns
  74. TCL_OK.  If an error occurred (e.g. \fIvarName\fR specifies an element
  75. of an array, but the actual variable isn't an array) then TCL_ERROR
  76. is returned and an error message is left in \fIinterp->result\fR.
  77. .PP
  78. The \fIflags\fR argument to \fBTcl_TraceVar\fR indicates when the
  79. trace procedure is to be invoked and provides information
  80. for setting up the trace.  It consists of an OR-ed combination
  81. of any of the following values:
  82. .TP
  83. \fBTCL_GLOBAL_ONLY\fR
  84. Normally, the variable will be looked up at the current level of
  85. procedure call;  if this bit is set then the variable will be looked
  86. up at global level, ignoring any active procedures.
  87. .TP
  88. \fBTCL_TRACE_READS\fR
  89. Invoke \fIproc\fR whenever an attempt is made to read the variable.
  90. .TP
  91. \fBTCL_TRACE_WRITES\fR
  92. Invoke \fIproc\fR whenever an attempt is made to modify the variable.
  93. .TP
  94. \fBTCL_TRACE_UNSETS\fR
  95. Invoke \fIproc\fR whenever the variable is unset.
  96. A variable may be unset either explicitly by an \fBunset\fR command,
  97. or implicitly when a procedure returns (its local variables are
  98. automatically unset) or when the interpreter is deleted (all
  99. variables are automatically unset).
  100. .PP
  101. Whenever one of the specified operations occurs on the variable,
  102. \fIproc\fR will be invoked.
  103. It should have arguments and result that match the type
  104. \fBTcl_VarTraceProc\fR:
  105. .nf
  106. .RS
  107. typedef char *Tcl_VarTraceProc(
  108. .RS
  109. ClientData \fIclientData\fR,
  110. Tcl_Interp *\fIinterp\fR,
  111. char *\fIname1\fR,
  112. char *\fIname2\fR,
  113. int \fIflags\fR);
  114. .RE
  115. .RE
  116. .fi
  117. The \fIclientData\fP and \fIinterp\fP parameters will
  118. have the same values as those passed to \fBTcl_TraceVar\fR when the
  119. trace was created.
  120. \fIClientData\fR typically points to an application-specific
  121. data structure that describes what to do when \fIproc\fR
  122. is invoked.
  123. \fIName1\fR and \fIname2\fR give the name of the traced variable
  124. in the normal two-part form (see the description of \fBTcl_TraceVar2\fR
  125. below for details).
  126. \fIFlags\fR is an OR-ed combination of bits providing several
  127. pieces of information.
  128. One of the bits TCL_TRACE_READS, TCL_TRACE_WRITES, or TCL_TRACE_UNSETS
  129. will be set in \fIflags\fR to indicate which operation is being performed
  130. on the variable.
  131. The bit TCL_GLOBAL_ONLY will be set whenever the variable being
  132. accessed is a global one not accessible from the current level of
  133. procedure call:  the trace procedure will need to pass this flag
  134. back to variable-related procedures like \fBTcl_GetVar\fR if it
  135. attempts to access the variable.
  136. The bit TCL_TRACE_DESTROYED will be set in \fIflags\fR if the trace is
  137. about to be destroyed;  this information may be useful to \fIproc\fR
  138. so that it can clean up its own internal data structures (see
  139. the section TCL_TRACE_DESTROYED below for more details).
  140. Lastly, the bit TCL_INTERP_DESTROYED will be set if the entire
  141. interpreter is being destroyed.
  142. When this bit is set, \fIproc\fR must be especially careful in
  143. the things it does (see the section TCL_INTERP_DESTROYED below).
  144. The trace procedure's return value should normally be NULL;  see
  145. ERROR RETURNS below for information on other possibilities.
  146. .PP
  147. \fBTcl_UntraceVar\fR may be used to remove a trace.
  148. If the variable specified by \fIinterp\fR, \fIvarName\fR, and \fIflags\fR
  149. has a trace set with \fIflags\fR, \fIproc\fR, and
  150. \fIclientData\fR, then the corresponding trace is removed.
  151. If no such trace exists, then the call to \fBTcl_UntraceVar\fR
  152. has no effect.
  153. The same bits are valid for \fIflags\fR as for calls to \fBTcl_TraceVar\fR.
  154. .PP
  155. \fBTcl_VarTraceInfo\fR may be used to retrieve information about
  156. traces set on a given variable.
  157. The return value from \fBTcl_VarTraceInfo\fR is the \fIclientData\fR
  158. associated with a particular trace.
  159. The trace must be on the variable specified by the \fIinterp\fR,
  160. \fIvarName\fR, and \fIflags\fR arguments (only the TCL_GLOBAL_ONLY
  161. bit from \fIflags\fR is used;  other bits are ignored) and its trace procedure
  162. must the same as the \fIproc\fR argument.
  163. If the \fIprevClientData\fR argument is NULL then the return
  164. value corresponds to the first (most recently created) matching
  165. trace, or NULL if there are no matching traces.
  166. If the \fIprevClientData\fR argument isn't NULL, then it should
  167. be the return value from a previous call to \fBTcl_VarTraceInfo\fR.
  168. In this case, the new return value will correspond to the next
  169. matching trace after the one whose \fIclientData\fR matches
  170. \fIprevClientData\fR, or NULL if no trace matches \fIprevClientData\fR
  171. or if there are no more matching traces after it.
  172. This mechanism makes it possible to step through all of the
  173. traces for a given variable that have the same \fIproc\fR.
  174.  
  175. .SH "TWO-PART NAMES"
  176. .PP
  177. The procedures \fBTcl_TraceVar2\fR, \fBTcl_UntraceVar2\fR, and
  178. \fBTcl_VarTraceInfo2\fR are identical to \fBTcl_TraceVar\fR,
  179. \fBTcl_UntraceVar\fR, and \fBTcl_VarTraceInfo\fR, respectively,
  180. except that the name of the variable has already been
  181. separated by the caller into two parts.
  182. \fIName1\fR gives the name of a scalar variable or array,
  183. and \fIname2\fR gives the name of an element within an
  184. array.
  185. If \fIname2\fR is NULL it means that either the variable is
  186. a scalar or the trace is to be set on the entire array rather
  187. than an individual element (see WHOLE-ARRAY TRACES below for
  188. more information).
  189.  
  190. .SH "ACCESSING VARIABLES DURING TRACES"
  191. .PP
  192. During read and write traces, the
  193. trace procedure can read, write, or unset the traced
  194. variable using \fBTcl_GetVar2\fR, \fBTcl_SetVar2\fR, and
  195. other procedures.
  196. While \fIproc\fR is executing, traces are temporarily disabled
  197. for the variable, so that calls to \fBTcl_GetVar2\fR and
  198. \fBTcl_SetVar2\fR will not cause \fIproc\fR or other trace procedures
  199. to be invoked again.
  200. Disabling only occurs for the variable whose trace procedure
  201. is active;  accesses to other variables will still be traced.
  202. .VS
  203. However, if a variable is unset during a read or write trace then unset
  204. traces will be invoked.
  205. .VE
  206. .PP
  207. During unset traces the variable has already been completely
  208. expunged.
  209. It is possible for the trace procedure to read or write the
  210. variable, but this will be a new version of the variable.
  211. Traces are not disabled during unset traces as they are for
  212. read and write traces, but existing traces have been removed
  213. from the variable before any trace procedures are invoked.
  214. If new traces are set by unset trace procedures, these traces
  215. will be invoked on accesses to the variable by the trace
  216. procedures.
  217.  
  218. .SH "CALLBACK TIMING"
  219. .PP
  220. When read tracing has been specified for a variable, the trace
  221. procedure will be invoked whenever the variable's value is
  222. read.  This includes \fBset\fR Tcl commands, \fB$\fR-notation
  223. in Tcl commands, and invocations of the \fBTcl_GetVar\fR
  224. and \fBTcl_GetVar2\fR procedures.
  225. \fIProc\fR is invoked just before the variable's value is
  226. returned.
  227. It may modify the value of the variable to affect what
  228. is returned by the traced access.
  229. .VS
  230. If it unsets the variable then the access will return an error
  231. just as if the variable never existed.
  232. .VE
  233. .PP
  234. When write tracing has been specified for a variable, the
  235. trace procedure will be invoked whenever the variable's value
  236. is modified.  This includes \fBset\fR commands\fR,
  237. commands that modify variables as side effects (such as
  238. \fBcatch\fR and \fBscan\fR), and calls to the \fBTcl_SetVar\fR
  239. and \fBTcl_SetVar2\fR procedures).
  240. \fIProc\fR will be invoked after the variable's value has been
  241. modified, but before the new value of the variable has been
  242. returned.
  243. It may modify the value of the variable to override the change
  244. and to determine the value actually returned by the traced
  245. access.
  246. .VS
  247. If it deletes the variable then the traced access will return
  248. an empty string.
  249. .VE
  250. .PP
  251. When unset tracing has been specified, the trace procedure
  252. will be invoked whenever the variable is destroyed.
  253. The traces will be called after the variable has been
  254. completely unset.
  255.  
  256. .SH "WHOLE-ARRAY TRACES"
  257. .PP
  258. If a call to \fBTcl_TraceVar\fR or \fBTcl_TraceVar2\fR specifies
  259. the name of an array variable without an index into the array,
  260. then the trace will be set on the array as a whole.
  261. This means that \fIproc\fR will be invoked whenever any
  262. element of the array is accessed in the ways specified by
  263. \fIflags\fR.
  264. When an array is unset, a whole-array trace will be invoked
  265. just once, with \fIname1\fR equal to the name of the array
  266. and \fIname2\fR NULL;  it will not be invoked once for each
  267. element.
  268.  
  269. .SH "MULTIPLE TRACES"
  270. .PP
  271. It is possible for multiple traces to exist on the same variable.
  272. When this happens, all of the trace procedures will be invoked on each
  273. access, in order from most-recently-created to least-recently-created.
  274. When there exist whole-array traces for an array as well as
  275. traces on individual elements, the whole-array traces are invoked
  276. before the individual-element traces.
  277. .VS
  278. If a read or write trace unsets the variable then all of the unset
  279. traces will be invoked but the remainder of the read and write traces
  280. will be skipped.
  281. .VE
  282.  
  283. .SH "ERROR RETURNS"
  284. .PP
  285. Under normal conditions trace procedures should return NULL, indicating
  286. successful completion.
  287. If \fIproc\fR returns a non-NULL value it signifies that an
  288. error occurred.
  289. The return value must be a pointer to a static character string
  290. containing an error message.
  291. If a trace procedure returns an error, no further traces are
  292. invoked for the access and the traced access aborts with the
  293. given message.
  294. Trace procedures can use this facility to make variables
  295. read-only, for example (but note that the value of the variable
  296. will already have been modified before the trace procedure is
  297. called, so the trace procedure will have to restore the correct
  298. value).
  299. .PP
  300. The return value from \fIproc\fR is only used during read and
  301. write tracing.
  302. During unset traces, the return value is ignored and all relevant
  303. trace procedures will always be invoked.
  304.  
  305. .SH "RESTRICTIONS"
  306. .PP
  307. A trace procedure can be called at any time, even when there
  308. is a partially-formed result in the interpreter's result area.  If
  309. the trace procedure does anything that could damage this result (such
  310. as calling \fBTcl_Eval\fR) then it must save the original values of
  311. the interpreter's \fBresult\fR and \fBfreeProc\fR fields and restore
  312. them before it returns.
  313.  
  314. .SH "UNDEFINED VARIABLES"
  315. .PP
  316. It is legal to set a trace on an undefined variable.
  317. The variable will still appear to be undefined until the
  318. first time its value is set.
  319. If an undefined variable is traced and then unset, the unset will fail
  320. with an error (``no such variable''), but the trace
  321. procedure will still be invoked.
  322.  
  323. .SH "TCL_TRACE_DELETED FLAG"
  324. .PP
  325. In an unset callback to \fIproc\fR, the TCL_TRACE_DELETED bit
  326. is set in \fIflags\fR if the trace is being removed as part
  327. of the deletion.
  328. Traces on a variable are always removed whenever the variable
  329. is deleted;  the only time TCL_TRACE_DELETED isn't set is for
  330. a whole-array trace invoked when only a single element of an
  331. array is unset.
  332.  
  333. .SH "TCL_INTERP_DESTROYED"
  334. .PP
  335. When an interpreter is destroyed, unset traces are called for
  336. all of its variables.
  337. The TCL_INTERP_DESTROYED bit will be set in the \fIflags\fR
  338. argument passed to the trace procedures.
  339. Trace procedures must be extremely careful in what they do if
  340. the TCL_INTERP_DESTROYED bit is set.
  341. It is not safe for the procedures to invoke any Tcl procedures
  342. on the interpreter, since its state is partially deleted.
  343. All that trace procedures should do under these circumstances is
  344. to clean up and free their own internal data structures.
  345.  
  346. .SH BUGS
  347. .PP
  348. Tcl doesn't do any error checking to prevent trace procedures
  349. from misusing the interpreter during traces with TCL_INTERP_DESTROYED
  350. set.
  351.  
  352. .SH KEYWORDS
  353. clientData, trace, variable
  354.